﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.ServiceModel;
using BookClubIt.Processor;

namespace BookClubIt
{
    public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (!Page.IsPostBack)
            {
            BindData();
            }
        }

        private void BindData()
        {
            Page.MetaDescription = "Create, Manage, and Discuss with your very own online book club. Register for free and create a book club in minutes at bookclubit.com!";
            Page.MetaKeywords = "book club, book clubs, bookclub, create online book club, create book clubs, book club management, book club books";

                DTO.BookClubs clubs = BookClubProcessorInstance.GetTopBookClubs();
                rptNewBookClubs.DataSource = clubs;
                rptNewBookClubs.DataBind();

                DTO.BookClubs pop_clubs = BookClubProcessorInstance.GetPopularBookClubs();
                rptPopularBookClubs.DataSource = pop_clubs;
                rptPopularBookClubs.DataBind();
        }


        protected void rptNewBookClubs_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {


            RepeaterItem item = e.Item; if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                DTO.BookClub club = e.Item.DataItem as DTO.BookClub;
                HyperLink lnkBookClub = (HyperLink)item.FindControl("lnkBookClub");
                Image imgClubAvatar = (Image)item.FindControl("imgClubAvatar");
                Label lblMemberCount = (Label)item.FindControl("lblMemberCount");
                Label lblCurrentBook = (Label)item.FindControl("lblCurrentBook");

                imgClubAvatar.ImageUrl = "avatars/club/" + club.AvatarURL;
                lnkBookClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;
                lnkBookClub.Text = club.Name;

                DTO.Members members = MemberProcessorInstance.GetBookClubMembersById(club.Id);

                lblMemberCount.Text = members.Count.ToString() + " members";

                DTO.Book book = BookProcessorInstance.GetCurrentBook(club.Id);

                if (book.Id != 0)
                {
                    lblCurrentBook.Text = "<b>Currently reading:</b> " + book.Title;
                }
                else
                {
                    lblCurrentBook.Text = "";
                }
            }

        }

        protected void rptPopularBookClubs_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {


            RepeaterItem item = e.Item; if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                DTO.BookClub pop_club = e.Item.DataItem as DTO.BookClub;
                HyperLink lnkBookClub = (HyperLink)item.FindControl("lnkBookClub");
                Image imgClubAvatar = (Image)item.FindControl("imgClubAvatar");
                Label lblMemberCount = (Label)item.FindControl("lblMemberCount");
                Label lblCurrentBook = (Label)item.FindControl("lblCurrentBook");

                imgClubAvatar.ImageUrl = "avatars/club/" + pop_club.AvatarURL;
                lnkBookClub.NavigateUrl = "bookclub/default.aspx?id=" + pop_club.Id;
                lnkBookClub.Text = pop_club.Name;

                DTO.Members members = MemberProcessorInstance.GetBookClubMembersById(pop_club.Id);

                lblMemberCount.Text = members.Count.ToString() + " members";

                DTO.Book book = BookProcessorInstance.GetCurrentBook(pop_club.Id);
                if (book.Id != 0)
                {
                    lblCurrentBook.Text = "<b>Currently reading:</b> " + book.Title;
                }
                else
                {
                    lblCurrentBook.Text = "";
                }
    
            }

        }

        static string TimeAgo(DateTime time)
        {
            DateTime startDate = DateTime.Now;
            TimeSpan deltaMinutes = startDate.Subtract(time);
            string distance = string.Empty;
            if (deltaMinutes.TotalMinutes <= (8724 * 60))
            {
                distance = DistanceOfTimeInWords(deltaMinutes.TotalMinutes);
                if (deltaMinutes.Minutes < 0)
                {
                    return distance + " from  now";
                }
                else
                {
                    return distance + " ago";
                }
            }
            else
            {
                return "on " + time.ToString();
            }
        }
        static string DistanceOfTimeInWords(double minutes)
        {
            if (minutes < 1)
            {
                return "less than a minute";
            }
            else if (minutes < 50)
            {
                return Math.Round(minutes).ToString() + " minutes";
            }
            else if (minutes < 90)
            {
                return "about one hour";
            }
            else if (minutes < 1080)
            {
                return Math.Round(new decimal((minutes / 60))).ToString() + " hours";
            }
            else if (minutes < 1440)
            {
                return "one day";
            }
            else if (minutes < 2880)
            {
                return "about one day";
            }
            else
            {
                return Math.Round(new decimal((minutes / 1440))).ToString() + " days";
            }
        }
    }
}

